package com.group9.handinhand.utils;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.jayway.jsonpath.JsonPath;


public class SemanticSearch {
	
	public ArrayList<String> doSemanticSearch(ArrayList<String> searchTags) {
		
		ArrayList<String> resultList = new ArrayList<String>();
		
		try {
			HttpTransport httpTransport = new NetHttpTransport();
			HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
			JSONParser parser = new JSONParser();
			
			ArrayList<String> parentTags = new ArrayList<String>();
			
			for (String string : searchTags) {
				GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/search");
				url.put("filter", "(all broader_than:" + string + ")");
				url.put("scoring", "freebase");
				url.put("limit", "10");
				url.put("indent", "true");
				url.put("key", "AIzaSyDDPyJ-KKTQY_saoJYKbYdRz6G6q0EtX6A");
				HttpRequest request = requestFactory.buildGetRequest(url);
				HttpResponse httpResponse = request.execute();
				JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
				JSONArray results = (JSONArray) response.get("result");
				for (Object result : results) {
					System.out.println(JsonPath.read(result, "$.name").toString());
					String result2 = JsonPath.read(result, "$.name").toString().replace(',', ' ');
					String[] words = StringUtils.split(result2, ' ');
					for (String word : words) {
						if(!word.equals(string))
						parentTags.add(word);
						resultList.add(word);
					}
				}
			}
			for (String string : parentTags) {
				GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/search");
				url.put("filter", "(all narrower_than:" + string + ")");
				url.put("scoring", "freebase");
				url.put("limit", "10");
				url.put("indent", "true");
				url.put("key", "AIzaSyDDPyJ-KKTQY_saoJYKbYdRz6G6q0EtX6A");
				HttpRequest request = requestFactory.buildGetRequest(url);
				HttpResponse httpResponse = request.execute();
				JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
				JSONArray results = (JSONArray) response.get("result");
				for (Object result : results) {
					System.out.println(JsonPath.read(result, "$.name").toString());
					String result2 = JsonPath.read(result, "$.name").toString().replace(',', ' ');
					String[] words = StringUtils.split(result2, ' ');
					for (String word : words) {
						if(!word.equals(string) && !searchTags.contains(word));
						resultList.add(word);
					}
				}
			}
			for (String string : searchTags) {
				GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/search");
				url.put("filter", "(all narrower_than:" + string + ")");
				url.put("scoring", "freebase");
				url.put("limit", "10");
				url.put("indent", "true");
				url.put("key", "AIzaSyCRwgzOqS0D2C_1_DAqTY-CgrFn67X4j1I");
				HttpRequest request = requestFactory.buildGetRequest(url);
				HttpResponse httpResponse = request.execute();
				JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
				JSONArray results = (JSONArray) response.get("result");
				for (Object result : results) {
					System.out.println(JsonPath.read(result, "$.name").toString());
					String result2 = JsonPath.read(result, "$.name").toString().replace(',', ' ');
					String[] words = StringUtils.split(result2, ' ');
					for (String word : words) {
						if(!word.equals(string))
						resultList.add(word);
					}
				}
			}
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return resultList;
	}

}
